﻿using software.common.def;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Reflection;


namespace project.B12.Model.Common
    {
        public static class EnumUtil
        {
            /// <summary>
            ///
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="value"></param>
            /// <returns></returns>
            public static string GetEnumName<T>(T value)
            {
                return Enum.GetName(typeof(T), value);
            }

            public static string ExplicateErrorCode(uint code)
            {
                string explicate = code.ToString();
                try
                {
                    explicate += ("," + GetStrFromInt<ErrCode>((int)code));
                }
                catch (Exception) { }
                return explicate;
            }

            /// <summary>
            /// 根据整数返回枚举
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="value"></param>
            /// <returns></returns>
            public static Enum GetEnumFromInt<T>(this int value)
            {
                return (Enum)Enum.Parse(typeof(T), Enum.GetName(typeof(T), value));
            }

            /// <summary>
            /// 根据整数返回字符串
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="value"></param>
            /// <returns></returns>
            public static string GetStrFromInt<T>(this int value)
            {
                return Enum.GetName(typeof(T), value);
            }

            /// <summary>
            /// 根据传入的int返回对应的描述
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="value"></param>
            /// <returns></returns>
            public static string getDescriptionFromInt<T>(this int value)
            {
                return GetDescriptionFromEnum(GetEnumFromInt<T>(value));
            }

            /// <summary>
            /// 根据传入的enum返回对应的描述
            /// </summary>
            /// <param name="en"></param>
            /// <returns></returns>
            public static string GetDescriptionFromEnum(Enum en)
            {
                if (en == null) return string.Empty;
                string value = en.ToString();
                FieldInfo field = en.GetType().GetField(value);
                object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);  //获取描述属性
                if (objs == null || objs.Length == 0)  //当描述属性没有时，直接返回名称
                    return "";
                DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0];
                return descriptionAttribute.Description;
            }

            /// <summary>
            /// 返回 Dic<Enum，Description>
            /// </summary>
            /// <param name="enumType"></param>
            /// <returns>Dic<Enum，Description></returns>
            public static Dictionary<string, string> GetEnumDic(Type enumType)
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                FieldInfo[] fieldinfos = enumType.GetFields();
                foreach (FieldInfo field in fieldinfos)
                {
                    if (field.FieldType.IsEnum)
                    {
                        Object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false);

                        dic.Add(field.Name, ((DescriptionAttribute)objs[0]).Description);
                    }
                }

                return dic;
            }

            /// <summary>
            /// 获得某个枚举项的描述
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            public static string GetEnumDes(object value)
            {
                FieldInfo fieldinfo = value.GetType().GetField(value.ToString());
                Object[] objs = fieldinfo.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
                if (objs == null || objs.Length == 0)
                {
                    return value.ToString();
                }
                else
                {
                    return ((DescriptionAttribute)objs[0]).Description;
                }
            }


            /// <summary>
            /// 获取枚举值上的Display特性的说明
            /// </summary>
            /// <typeparam name="T">枚举类型</typeparam>
            /// <param name="obj">枚举值</param>
            /// <returns>特性的说明</returns>
            public static string GetEnumDisplay(object value)
            {
                FieldInfo field = value.GetType().GetField(value.ToString());
                DisplayAttribute descAttr = Attribute.GetCustomAttribute(field, typeof(DisplayAttribute)) as DisplayAttribute;
                if (descAttr == null)
                {
                    Object[] objs = field.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false);
                    if (objs == null || objs.Length == 0)
                    {
                        return value.ToString();
                    }
                    else
                    {
                        return ((DescriptionAttribute)objs[0]).Description;
                    }
                }

                return descAttr.GetName();
            }

            /// <summary>
            /// 返回 Dic<Enum，Description>
            /// </summary>
            /// <param name="enumType"></param>
            /// <returns>Dic<Enum，Description></returns>
            public static Dictionary<string, string> GetEnumDisplay(Type enumType)
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                FieldInfo[] fieldinfos = enumType.GetFields();
                foreach (FieldInfo field in fieldinfos)
                {
                    if (field.FieldType.IsEnum)
                    {
                        DisplayAttribute descAttr = Attribute.GetCustomAttribute(field, typeof(DisplayAttribute)) as DisplayAttribute;
                        dic.Add(field.Name, descAttr.GetName());
                    }
                }

                return dic;
            }


        /// <summary>
        /// 返回 Dic<Enum，Description>
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns>Dic<Enum，Description></returns>
        public static Dictionary<string, T> GetEnumDisplay<T>(Type enumType)
        {
            Dictionary<string, T> dic = new Dictionary<string, T>();
            FieldInfo[] fieldinfos = enumType.GetFields();
            foreach (FieldInfo field in fieldinfos)
            {
                if (field.FieldType.IsEnum)
                {
                    T value = (T)field.GetValue(null);
                    DisplayAttribute DisplayAttr = Attribute.GetCustomAttribute(field, typeof(DisplayAttribute)) as DisplayAttribute;
                    //dic.Add(field.Name, DisplayAttr.GetName());
                    dic.Add(DisplayAttr.GetName(), value);
                }
            }

            return dic;
        }

        /// <summary>
        /// 返回 Dic<枚举Key，枚举类型>
        /// </summary>
        /// <param name="enumType"></param>
        /// <returns>Dic<枚举Key，枚举类型></returns>
        public static Dictionary<string, T> GetEnumDic<T>(Type enumType)
        {
            Dictionary<string, T> dic = new Dictionary<string, T>();
            FieldInfo[] fieldinfos = enumType.GetFields();
            foreach (FieldInfo field in fieldinfos)
            {
                if (field.FieldType == enumType && field.IsLiteral)
                {
                    string name = field.Name;
                    T value = (T)field.GetValue(null);
                    DescriptionAttribute resStr = (DescriptionAttribute)Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute));
                    if (resStr != null)
                    {
                        string key = resStr.Description;
                        dic[key] = value;
                    }
                }
            }
            return dic;
        }
    }
    }

